nitobi.grid.Grid.prototype.setupGridScrollPosition = function(pos)
{
	var posInfo = pos.split(",");
	if(posInfo.length == 2)
	{
		this.vScrollbar.setScrollPercent(posInfo[0]);
		this.hScrollbar.setScrollPercent(posInfo[1]);
	}
}

nitobi.grid.Grid.prototype.processGridSelected = function(selectedRows)
{
	this.clearActiveRows();
	if(selectedRows.length != 0)
	{
		var rowIndexes = selectedRows.split(",");
		for(var p = 0; p < rowIndexes.length; p++)
			this.selectGridRow(rowIndexes[p],true);			
	}
}

nitobi.grid.Grid.prototype.selectGridRow = function(rowIndex,forceActive)
{
	var rows = this.getSelectedRows();
	var givenRow = nitobi.grid.Row.getRowElement(this, rowIndex);
	if(givenRow != null)
	{
		var setActive = true;
		for(var p = 0; p < rows.length;p++)
			if(rows[p] == givenRow)
			{
				setActive = false;
				break;
			}
		
		if(setActive)
		{
			this.setActiveRow(givenRow, true);
			this.ensureCellInView(this.getCellElement(rowIndex, 1));
		}
		else if(!forceActive)
			this.clearActiveRow(givenRow);
	}
}

nitobi.grid.OnAfterSaveEventArgs = function(source, responseData, success) 
{ 
nitobi.grid.OnAfterSaveEventArgs.baseConstructor.call(this,source); 
this.success = success; 
this.responseData = responseData; 
}; 
nitobi.lang.extend(nitobi.grid.OnAfterSaveEventArgs, nitobi.data.DataTableEventArgs); 

nitobi.grid.OnAfterSaveEventArgs.prototype.getResponseData = function() 
{ 
return this.responseData; 
}; 

nitobi.grid.OnAfterSaveEventArgs.prototype.getSuccess = function() 
{ 
return this.success; 
};

function setGridState(stateEl)
{
	if(stateEl.getAttribute('refresh') != null)
		eval(stateEl.getAttribute('refresh'));
	if(stateEl.getAttribute('rowCount') != null)
	{
		var rc = stateEl.getAttribute('rowCount');
		this.dhtmlObj.setRowCount(rc);
	}
}

function getGridState(stateEl)
{
	this.dhtmlObj.DoneSetupAlready = false;
	stateEl.setAttribute('selectedRows', this.selRowCtl.value);
	stateEl.setAttribute('xt', this.xt);
	stateEl.setAttribute('ls', this.ls);
	if(this.dhtmlObj.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
	{
		stateEl.setAttribute('updates',this.dhtmlObj.datatable.log.xml);
		this.dhtmlObj.datatable.flushLog();
	}
}

function gridCellFocus(eventArgs)
{
	var grid = eventArgs.getSource();
    var cell = eventArgs.getCell();
	var row = cell.getRow();	
	if(grid.syrinxLastRow != row)
	{
	    grid.syrinxLastRow = row;
	    if(grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
		    grid.BizProc.triggerEvent(grid.ClientControl.id, 'bizObjUpdated');
    }
}

function gridCellEditComplete(eventArgs)
{
	try
	{
	//debugger;
		if(eventArgs.getEvent().type == "blur" || eventArgs.getEvent().type == "keydown")
		{										   
			var cell = eventArgs.getCell();
			var col = cell.getColumn();
			var row = cell.getRow();
			var grid = eventArgs.getSource();
			var backward = eventArgs.getEvent().shiftKey;
			var keyCode = eventArgs.getEvent().keyCode;
			//var realBlur = true;
			var doUpdate = false;
															   
			//debugger;
			
			if(eventArgs.getEvent().type == "blur")
			{
				var lastKey = -1;
				try{lastKey = grid.cellEditor.lastKeyCode;}catch(e){}
				if(lastKey == 13)
					doUpdate = true;
			}
			else if(keyCode == 38 || keyCode == 40 || keyCode == 13)
				doUpdate = true;
						
			if(doUpdate && grid.datatable.log.selectNodes("//"+nitobi.xml.nsPrefix+"data/*").length !=0)
				grid.BizProc.triggerEvent(grid.ClientControl.id, 'bizObjUpdated');
		}
	}
	catch(e) {}
}

function extendedAfterSave(grid, row, col, backward)
{
	grid.subscribeOnce('AfterRefresh', 
		function(grid, row, col, backward)
		{
			row = row + ((backward)?-1:1);
			for(col = 0; col <= grid.columns.length; col++)
			{
				var column = new nitobi.grid.Column(grid, col);
				if(column.isEditable())
				{
					//setTimeout("setSelectedGridCell('" + grid.uid + "'," + row + "," + col + ")", 0);
					return;
				}
			}
		}
	, null, new Array(grid, row, col, backward));
	grid.refresh();
}

function setSelectedGridCell(gridId, row, col)
{
	var grid = nitobi.getGrid(gridId);
	grid.selectCellByCoords(row, col);	
//	grid.activeCell.focus();
}